package com.fengshang.wedding.domain.model.internal;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import com.fengshang.wedding.domain.Constants;
import com.fengshang.wedding.domain.model.api.Gender;
import com.fengshang.wedding.domain.model.api.IUser;
import com.fengshang.wedding.domain.model.api.Role;
import com.fengshang.wedding.domain.model.api.User;

/**
 * User Entity
 * 
 * @author gembin@gmail.com
 * 
 */

@Entity(name = Constants.USER_ENTITY_NAME)
@NamedQueries( {
		@NamedQuery(name = Constants.USER_FIND_ALL, query = "SELECT u FROM User u") 
})

public class UserEntity implements IUser, Serializable {
	private static final long serialVersionUID = 3447681945941024055L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;

	@Column(name = "name", unique = true, nullable = false)
	private String name;

	@Column(name = "password", nullable = false)
	private String password;

	@Column(name = "email", nullable = false)
	private String email;

	@Enumerated(EnumType.ORDINAL)
	private Role role;
	
	@Basic
	private String qq;

	@Basic
	private String mobile;

	@Basic
	private String phone;

	@Enumerated(EnumType.ORDINAL)
	private Gender gender;

	@Basic
	private String website;
	/**
	 * indicates TOPIC.USER_ID is the column to match against USER.ID to retrieve topics associated with a user. 
	 */
	@OneToMany(cascade = CascadeType.ALL,mappedBy = "owner")
	private Collection<TopicEntity> topics = new ArrayList<TopicEntity>(1);

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}


	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public String getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website = website;
	}
	
	public void setRole(Role role) {
		this.role = role;
	}

	public Role getRole() {
		return role;
	}
	
	public void addTopic(TopicEntity topic){
		if(!topics.contains(topic)){
			topic.setOwner(this);
			topics.add(topic);
		}
	}
	
	public Collection<TopicEntity> getTopics() {
		return topics;
	}

	public void setTopics(Collection<TopicEntity> topics) {
		this.topics = topics;
	}

	@Transient
	public UserEntity importData(IUser user) {
		if (user != null){
			this.role = user.getRole()==null?role:user.getRole();
			this.email =user.getEmail()==null?email:user.getEmail();
			this.gender = user.getGender()==null?gender:user.getGender();
			this.name = user.getName()==null?name:user.getName();
			this.password = user.getPassword()==null?password:user.getPassword();
			this.phone = user.getPhone()==null?phone:user.getPhone();
			this.mobile = user.getMobile()==null?mobile:user.getMobile();
			this.qq = user.getQq()==null?qq:user.getQq();
			this.website = user.getWebsite()==null?website:user.getWebsite();
		}
		return this;
	}

	@Transient
	public IUser getValueObject() {
		return new User(this);
	}
	

}
